package com.invoicerecognition.backend.handler;

import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Set;

public class JsonSetTypeHandler extends AbstractJsonTypeHandler<Set<String>> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public JsonSetTypeHandler() {
        super(Set.class); // 传递 Set.class
    }

    @Override
    public Set<String> parse(String json) {
        try {
            // 使用 TypeReference 解析具体的泛型信息
            return objectMapper.readValue(json, new TypeReference<Set<String>>() {});
        } catch (Exception e) {
            throw new RuntimeException("Error parsing JSON to Set<String>", e);
        }
    }

    @Override
    public String toJson(Set<String> set) {
        try {
            return objectMapper.writeValueAsString(set);
        } catch (Exception e) {
            throw new RuntimeException("Error converting Set<String> to JSON", e);
        }
    }
}

